#!/usr/bin/evn python
# -*-coding:utf-8 -*-

import time

import allure
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions

from ..base.base_page import BasePage


class AddMemberPage(BasePage):
    __NAME = (By.ID, 'username')
    __MID = (By.ID, 'memberAdd_acctid')
    __PHONE_NUM = (By.ID, 'memberAdd_phone')
    __SEND_INVITE = (By.NAME, "sendInvite")
    __CONTINUE_BUTTON = (By.CSS_SELECTOR, '.qui_btn.ww_btn.ww_btn_Blue.js_btn_continue')
    __SAVE_BUTTON = (By.CSS_SELECTOR, '.qui_btn.ww_btn.js_btn_save')
    __CANCEL_BUTTON = (By.CSS_SELECTOR, '.qui_btn.ww_btn.js_btn_cancel')
    __MID_ERR_TIPS = (By.XPATH,"//*[@class='member_edit_item member_edit_item_Account']//*["
                                     "@class='ww_inputWithTips_tips']")
    __PHONE_ERR_TIPS = (By.CSS_SELECTOR, ".ww_telInput~.ww_inputWithTips_tips")

    def input_member_info(self, mname, mid, phone_num):
        '''
        填写成员信息
        :return:
        '''
        with allure.step("添加成员信息"):
            self.wait_until(expected_conditions.visibility_of_element_located(self.__NAME))
            # 姓名
            name_ele = self.find_ele(self.__NAME)
            name_ele.clear()
            name_ele.send_keys(mname)
            # 账号
            mid_ele = self.find_ele(self.__MID)
            mid_ele.clear()
            mid_ele.send_keys(mid)
            # 手机号
            phone_ele = self.find_ele(self.__PHONE_NUM)
            phone_ele.clear()
            phone_ele.send_keys(phone_num)
        # 关闭自动发送邀请通知
        with allure.step("关闭自动发送邀请通知"):
            ele_send_invite = self.find_ele(self.__SEND_INVITE)
            if ele_send_invite.is_selected():
                ele_send_invite.click()

    def cancel_button(self):
        with allure.step("点击取消按钮"):
            self.find_eles(self.__CANCEL_BUTTON)[0].click()

    def save_button(self):
        with allure.step("点击保存按钮"):
            self.find_eles(self.__SAVE_BUTTON)[0].click()

    def continue_button(self):
        with allure.step("点击保存并继续添加按钮"):
            self.find_eles(self.__CONTINUE_BUTTON)[0].click()

    def save_member_continue(self, mname, mid, phone_num):
        self.input_member_info(mname, mid, phone_num)
        self.save_button()
        from .contact_page import ContactPage
        return ContactPage(self.driver)

    def save_add_member(self, mname, mid, phone_num):
        self.input_member_info(mname, mid, phone_num)
        self.save_button()
        from .contact_page import ContactPage
        return ContactPage(self.driver)

    def continue_add_member(self, mname, mname1, mid, phone_num):
        self.input_member_info(mname, mid, phone_num)
        self.continue_button()
        time.sleep(3)
        self.input_member_info(mname1, mid, phone_num)

    def cancel_add_member(self):
        self.cancel_button()
        from .contact_page import ContactPage
        return ContactPage(self.driver)

    def get_mid_error_tips(self):
        # 账号错误信息
        mid_err_tips = self.find_ele(self.__MID_ERR_TIPS).text
        return mid_err_tips

    def get_phone_err_tips(self):
        # 手机号重复信息
        phone_err_tips = self.find_ele(self.__PHONE_ERR_TIPS).text
        return phone_err_tips


